package com.sx.gongzhong.adapter;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sx.gongzhong.R;
import com.sx.gongzhong.model.ExposureBean;
import com.sx.gongzhong.net.ServiceFactory;
import com.sx.gongzhong.utils.BaseUtil;

import java.util.List;

/**
 * Created by XZY on 2017/12/28
 * 爆料的adapter
 */

public class ExposureListAdapter extends BaseQuickAdapter<ExposureBean,BaseViewHolder> {

    private Context context;

    public ExposureListAdapter(Context context, @LayoutRes int layoutResId, @Nullable List<ExposureBean> data) {
        super(layoutResId, data);
        this.context=context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final ExposureBean item) {
        helper.setText(R.id.tv_Name,item.getPublisher()!=null?item.getPublisher().getName():"");
        helper.setText(R.id.tv_Type,item.getBrokeNewsType()!=null?"#"+item.getBrokeNewsType().getName():"");
        helper.setText(R.id.tv_Title,item.getTitle());
        helper.setText(R.id.tv_Content,item.getContent());
        helper.setText(R.id.tv_praise,item.getPraiseTimes()+"");
        helper.setText(R.id.tv_comment,item.getReplyTimes()+"");
        if (item.getPublisher()!=null&& !BaseUtil.isEmpty(item.getPublisher().getHeaderUrl())){
            Glide.with(context).load(ServiceFactory.getUrl(item.getPublisher().getHeaderUrl()))
                    .apply(new RequestOptions().centerCrop().optionalCircleCrop().
                            placeholder(R.mipmap.head_default_80).error(R.mipmap.head_default_80))
                    .into((ImageView)helper.getView(R.id.im_Head));

        }
        ImageView image1=helper.getView(R.id.imageView1);
        LinearLayout ll2=helper.getView(R.id.ll2);
        LinearLayout ll3=helper.getView(R.id.ll3);
        TextView tv_Num=helper.getView(R.id.tv_Num);
        tv_Num.setVisibility(View.GONE);
        image1.setVisibility(View.GONE);
        ll2.setVisibility(View.GONE);
        ll3.setVisibility(View.GONE);
        RequestOptions options = new RequestOptions().centerCrop()
                .placeholder(R.mipmap.img_fail).error(R.mipmap.img_fail);
        if (item.getFiles()!=null){
           int count =item.getFiles().size();
            if (count==1){
                image1.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(0).getPath()))
                        .apply(options)
                        .into(image1);
            }
            if (count==2){
                ll2.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(0).getPath()))
                        .apply(options)
                        .into((ImageView) helper.getView(R.id.imageView21));
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(1).getPath()))
                        .apply(options)
                        .into((ImageView) helper.getView(R.id.imageView22));
            }
            if (count>=3){
                if (count>3) {
                    tv_Num.setVisibility(View.VISIBLE);
                    tv_Num.setText("+ " + (count - 3));
                }
                ll3.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(0).getPath()))
                        .apply(options)
                        .into((ImageView)helper.getView(R.id.imageView31));
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(1).getPath()))
                        .apply(options)
                        .into((ImageView)helper.getView(R.id.imageView32));
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(2).getPath()))
                        .apply(options)
                        .into((ImageView)helper.getView(R.id.imageView33));
            }
        }

    }

}
